﻿@model SalePromotion.Models.SalePaginationListModel
@if (Model.HasItems)
{
    @Html.Partial("Pagination", Model.PagedItems)
    
    <table class="zebra-striped">
        <thead>
            <tr>
                <th>
                    Sale Name
                </th>
                <th>
                    Start Date
                </th>
                <th>
                    End Date
                </th>
                <th>
                    Discount
                </th>
                <th>
                    Shipping
                </th>
                <th>
                    Status
                </th>
                <th style="width: 140px">
                    Action
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.PagedItems)
            {
                <tr>
                    <td>@item.SaleName
                    </td>
                    <td>@String.Format("{0:g}", @item.StartDate)
                    </td>
                    <td>@String.Format("{0:g}", @item.EndDate)
                    </td>
                    <td>@item.DiscountText
                    </td>
                    <td>@item.FreeShippingText
                    </td>
                    <td>@item.StatusText
                    </td>
                    <td class="action">
                        @{
                      
                        
                           
                if (item.StatusText != "Active")
                {
                    using (Html.BeginForm("DeleteSalePromotion", "Home", new { id = item.Id, page = Model.PagedItems.CurrentPage, filter = Model.Filter }, FormMethod.Post, new { id = "deleteSalePromotionForm" }))
                    {
                            <input type="submit"  class="btn" style="display: none"/>
                            <input type="button"  class="btn hideDeleteBtn" value="Delete"/>
                    }
                }
                    <input type="submit" value="Edit" class="btn"  onclick="document.location.href='@Url.Action("SaleEdit", "Home", new { id = item.Id })'" />
                        }
                        <div class="clear">
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    @Html.Partial("Pagination", Model.PagedItems)

    <div id="ConfirmMessage" title="Confirm Deletion" style="display: none">
        <p id="ConfirmMessageBody" class="msgbody">
            Important: All listings associated with this sale will be removed from this sale.
            Are you sure you want to delete this sale?</p>
    </div>
}
else
{
    <p>
        <em>There are no Sale Promotions.</em></p>
}

    <script type="text/javascript">
        var currentDeleteButton = null;
        $(function () {
            $('input.hideDeleteBtn').click(function (e) {
                currentDeleteButton = $(this);
                $('#ConfirmMessage').dialog('open');
            });

            $('#ConfirmMessage').dialog({
                autoOpen: false,
                width: 300,
                modal: true,
                buttons: {
             
                    "OK": function () {
                        $(this).dialog('close');
                        submitDelete();
                    },
                           "Cancel": function () {
                        $(this).dialog('close');

                    }
                }
            });

        });

        function submitDelete() {
            if (currentDeleteButton) {
                $(currentDeleteButton).prev().click();
            }
        }
    </script>

